﻿using System;
using System.Net;
using System.Windows;
using SCG.SolarVengeanceEngine;
using System.Collections.Generic;
using SCG.General;

namespace SCG.SolarVengeance.Scenarios
{
    public class FarCapsClassic : Scenario
    {
        public override string Author
        {
            get
            {
                return "Ugly Green Things";
            }
        }

        public override string Description
        {
            get
            {
                return "This is a modified version of the SCG Classic scenario, which attempts to keep the capitals a reasonable distance" +
                    " from each other.  It also randomly adds Pulsars within the cluster and Black Holes outside the cluster" +
                    " when these options are enabled.  The Black Hole option makes skirting around the edge of the cluster a deadly" +
                    " risk.  Caution: Using a cluster density of 1 may result in starsystems being placed next to a black hole.";
            }
        }
        //Release Notes:  Currently the only remaining unedited code of SCG Classic is the Wormhole, and Nebula coding. 

        //--------------------Define parameters that can be modified by the setup page
        private ScenarioParameter paramStarSystems;
        private ScenarioParameter paramDensity;
        private ScenarioParameter enbBH;
        private ScenarioParameter enbPL;

        //--------------------Create instances of the parameter objects
        public FarCapsClassic()
        {
            paramStarSystems = CreateParameter("Number of Star Systems", 20, 2, 50);
            paramDensity = CreateParameter("Cluster Density", 2, 1, 10);
            enbBH = CreateParameter("Black Holes Enabled?", 1, 0, 1);
            enbPL = CreateParameter("Pulsars Enabled?", 1, 0, 1);
        }

        //-------------------Build the scenario with the specified number of players
        public override void BuildScenario(int numPlayers, int capitalValues, int startingResources)
        {
            //----------------------------Create Capital Systems---------------------------------------------------------------------------------
            int Stars = numPlayers;
            StarSystem[] dcapital = new StarSystem[11];
            int Capitals = 0;
            int CapX = 0;
            int CapY = 0;
            int loops = 0;
            SCGPoint ptCapitals = new SCGPoint();
            bool CapitalsOK = false;         

            while (Capitals < numPlayers)
            {
                do
                {
                    CapitalsOK = true;
                    loops++;
                    if (Capitals == 0)
                    {
                        ptCapitals = new SCGPoint(Random(20, 80), Random(20, 80));
                        double dist = Distance(ptCapitals.X, ptCapitals.Y, 50, 50);
                        CapitalsOK = dist < 30;
                    }
                    else
                    {
                        ptCapitals = new SCGPoint(Random(20, 80), Random(20, 80));
                        for (int d = 0; d < Capitals; d++)
                        {
                            CapX = dcapital[d].X;
                            CapY = dcapital[d].Y;
                            double dist = Distance(ptCapitals.X, ptCapitals.Y, CapX, CapY);
                            if (dist < 20)
                                CapitalsOK = false;
                        }
                    }
                }
                while (!CapitalsOK && loops < 1000000);
                if (loops >= 1000000)                
                    break;
                  
                dcapital[Capitals] = CreateStarSystem((int)ptCapitals.X, (int)ptCapitals.Y, 20, 10, 10, 20);
                AssignCapital(Game.Players[Capitals], new SCGRect(ptCapitals.X - 1, ptCapitals.Y - 1, 3, 3));
                Capitals++;
            }

            //--------------------Create StarCluster---------------------------------------------------------------------------------------------------

            CreateStarCluster(paramStarSystems.Value, 50, 50, 50, 50, 45, paramDensity.Value);

            //--------------------Create Random Nebula-------------------------------------------------------------------------------------------------
            int numNebula = SVGame.RNG.Next(4) + 2;

            while (numNebula > 0)
            {
                CreateNebula(SVGame.RNG.Next(40) + 30, SVGame.RNG.Next(40) + 30, SVGame.RNG.Next(5) + 4, SVGame.RNG.Next(40) + 30);
                numNebula--;
            }

            //--------------------Create Random Wormholes----------------------------------------------------------------------------------------------
            int numWormholes = SVGame.RNG.Next(4) + 1;

            while (numWormholes > 0)
            {
                CreateWormhole(SVGame.RNG.Next(100), SVGame.RNG.Next(100), SVGame.RNG.Next(40) + 30, SVGame.RNG.Next(40) + 30);
                numWormholes--;
            }
            numWormholes = SVGame.RNG.Next(4) + 1;
            while (numWormholes > 0)
            {
                CreateWormhole(SVGame.RNG.Next(40) + 30, SVGame.RNG.Next(40) + 30, SVGame.RNG.Next(100), SVGame.RNG.Next(100));
                numWormholes--;
            }

            //-------------------Create Random Pulsars within the cluster-------------------------------------------------------------------------------
            int pulsars = Random(numPlayers, (numPlayers + 4));

            while (pulsars > 0 && enbPL.Value == 1)
            {
                SCGPoint ptPulsar = new SCGPoint();
                ptPulsar = new SCGPoint(Random(25, 75), Random(25, 75));
                CreatePulsar((int)ptPulsar.X, (int)ptPulsar.Y);
                pulsars--;
            }

            //-------------------Create Black Holes just outside the cluster perimeter-------------------------------------------------------------------
            int BlackHolesCreated = 0;
            Dictionary<int, int> BHInfoXpos = new Dictionary<int, int>();
            Dictionary<int, int> BHInfoYpos = new Dictionary<int, int>();
            int bhX = 0;
            int bhY = 0;
            int BlackHoles = Random(numPlayers, (numPlayers + 10));

            while (BlackHoles > 0 && enbBH.Value == 1)
            {
                bool BlackHolesOK = false;
                SCGPoint ptBlackHoles = new SCGPoint();
                do
                {
                    ptBlackHoles = new SCGPoint(Random(0, 99), Random(0, 99));
                    double dist = Distance(ptBlackHoles.X, ptBlackHoles.Y, 50, 50);
                    BlackHolesOK = dist > 45;
                    if (BlackHolesOK == true)
                    {
                        if (BlackHolesCreated > 0)
                        {
                            for (int d = 0; d < BlackHolesCreated; d++)
                            {
                                bhX = BHInfoXpos[d];
                                bhY = BHInfoYpos[d];
                                double bhdist = Distance(ptBlackHoles.X, ptBlackHoles.Y, bhX, bhY);
                                if (bhdist < 10)
                                    BlackHolesOK = false;
                            }
                        }
                    }
                }
                while (!BlackHolesOK);
                CreateBlackHole((int)ptBlackHoles.X, (int)ptBlackHoles.Y, Random(10, 15));
                BHInfoXpos.Add(BlackHolesCreated, (int)ptBlackHoles.X);
                BHInfoYpos.Add(BlackHolesCreated, (int)ptBlackHoles.Y);
                BlackHolesCreated++;
                BlackHoles--;
            }
        }
    }
}
